package com.example.xjtuse_pc.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import org.apache.http.util.EncodingUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * 短信工具类
 */
public class SmsUtils {





    /**
     * 短信进度回调
     */
    public interface SmsBcakUp {
        /**
         * 进度条最大值
         *
         * @param max
         */
        public void beforeBackup(int max);

        /**
         * 备份过程中，进度条值
         *
         * @param progress
         */
        public void onSmsSetUp(int progress);
    }

    /**
     * 备份手机短信
     *
     * @param context
     */
    public void saveSms(Context context, SmsBcakUp sbp) throws Exception {
        ContentResolver cr = context.getContentResolver();
        File file = new File(Environment.getExternalStorageDirectory(), "SaveSms.xml");
        FileOutputStream fos = new FileOutputStream(file);
        if (!file.exists()) {
            file.createNewFile();
        }
        JSONArray jsonarray = new JSONArray();

        Uri uri = Uri.parse("content://sms/");
        Cursor cursor = cr.query(uri, new String[]{"body", "address", "date", "type"}, null, null, null);
        sbp.beforeBackup(cursor.getCount());
        int progress = 0;
        while (cursor.moveToNext()) {
            Thread.sleep(50);
            JSONObject jobject = new JSONObject();
            jobject.put("body", cursor.getString(0));
            jobject.put("address", cursor.getString(1));
            jobject.put("date", cursor.getString(2));
            jobject.put("type", cursor.getString(3));
            jsonarray.put(jobject);

            progress++;
            sbp.onSmsSetUp(progress);

        }
        String json = jsonarray.toString();
        Log.i("join",json);
        byte bytes[] = new byte[1024];
        bytes = json.getBytes();
        int b = json.length();
        fos.write(bytes, 0, b);
        fos.close();

    }


    public void getSms(Context context,SmsBcakUp sb) throws Exception {

        Uri uri = Uri.parse("content://sms/");

            context.getContentResolver().delete(uri, null, null);

        String res = "";
        ContentResolver cr = context.getContentResolver();
        File file = new File(Environment.getExternalStorageDirectory(), "SaveSms.xml");
        FileInputStream fis = new FileInputStream(file);
        int length = fis.available();
        byte[] buffer = new byte[length];
        fis.read(buffer);
        res = EncodingUtils.getString(buffer, "UTF-8");
        fis.close();
        Log.i("join", res.substring(0, 20));

        JSONArray jsonArray = new JSONArray(res);
        sb.beforeBackup(jsonArray.length());
        for (int i = 0; i < jsonArray.length(); i++) {
            Thread.sleep(50);
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String type = jsonObject.getString("type");
            String date = jsonObject.getString("date");
            String address = jsonObject.getString("address");
            String body = jsonObject.getString("body");
            Log.i("join", type + "," + date + "," + address + "," + body);
            ContentValues cv=new ContentValues();
            cv.put("type",jsonObject.getString("type"));
            cv.put("date",jsonObject.getString("date"));
            cv.put("address",jsonObject.getString("address"));
            cv.put("body",jsonObject.getString("body"));
            context.getContentResolver().insert(uri, cv);
            sb.onSmsSetUp(i);

        }

    }
}
